<template>
    <view class="container">
        <view class="login-container">
            <view class="login-title">
                <text>城市管理与运行安全创新<br/>研发中心系统</text>
            </view>
            <view class="input-group">
                <view class="input-item">
                    <text class="input-label">账号</text>
                    <input v-model="username" placeholder="请输入账号" class="input-field" />
                </view>
                <view class="input-item">
                    <text class="input-label">密码</text>
                    <input v-model="password" type="password" placeholder="请输入密码" class="input-field" />
                </view>
            </view>
            <button class="login-btn" @click="handleLogin">登录</button>
        </view>
    </view>
</template>

<script setup>
import { ref } from 'vue'
//api
import { commonApi } from "@/api/index.js";

const username = ref('')
const password = ref('')

const handleLogin = async () => {
    uni.showLoading({
        title: '登录中...'
    })
    const name= username.value.split('@')
    // 登录接口
    const res= await commonApi.login({
        username: name[0],
        password: password.value,
        tenantName: name[1] || '',
    })
    if(res.data){
        uni.setStorageSync('token', res.data.accessToken)
        uni.setStorageSync('userInfo', res.data)
        uni.showToast({
            title: '登录成功',
            icon: 'success'
        })
       setTimeout(() => {
        uni.reLaunch({
            url: '/pages/index/index'
        })
       }, 500);
    }else{
        uni.showToast({
            title: res.msg || '登录失败',
            icon: 'none'
        })
    }
    uni.hideLoading()
    
}
</script>

<style lang="scss" scoped>
.container {
    min-height: 100vh;
    background: #f5f7fa;
    display: flex;
    justify-content: center;
    align-items: center;
}

.login-container {
    background: #ffffff;
    padding: 60rpx;
    border-radius: 20rpx;
    width: 95%;
}

.login-title {
    text-align: center;
    margin-bottom: 60rpx;

    text {
        font-size: 40rpx;
        font-weight: 600;
        color: #333;
        line-height: 1.5;
        letter-spacing: 1rpx;
    }
}

.input-group {
    margin-bottom: 60rpx;
}

.input-item {
    margin-bottom: 40rpx;
}

.input-label {
    display: block;
    font-size: 28rpx;
    color: #666;
    margin-bottom: 16rpx;
}

.input-field {
    width: 100%;
    height: 80rpx;
    border: 2rpx solid #e0e0e0;
    border-radius: 8rpx;
    padding: 0 20rpx;
    font-size: 28rpx;
    color: #333;
}

.login-btn {
    width: 100%;
    height: 80rpx;
    background: #2196F3;
    color: #ffffff;
    border-radius: 8rpx;
    font-size: 32rpx;
    display: flex;
    justify-content: center;
    align-items: center;
}
</style>